/**
 *
 * Licensed Materials - Property of IBM 
 * 
 * OCO Source Materials 
 * 
 * (C) Copyright IBM Corp. 2011, 2012 All Rights Reserved 
 * 
 * The source code for this program is not published or other- 
 * wise divested of its trade secrets, irrespective of what has 
 * been deposited with the U.S. Copyright Office. 
 *
 */

/**
 * CHANGE HiSTORY:
 * Author             Time                 Description
 *
 */
package com.ibm.ba.demo.dao;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

import com.ibm.ba.demo.entity.BaDemoScoreResult;
import com.ibm.ba.demo.entity.Product;

/**
 * BatchScoreDao is to operate BatchScore table, like query, batch update,
 * delete.
 * 
 * @author Luyi
 * 
 */
@Repository
public class BatchScoreDao extends AbstractDao {
	private static Logger logger = Logger.getLogger(BatchScoreDao.class);
	private static String SQL_GET_SCORE = "SELECT T1.B_CUSTKEY,T1.B_PARTKEY,T2.P_NAME,T1.B_SCORE FROM BATCHSCORE T1, PARTCH T2 WHERE T1.B_CUSTKEY= :custId AND T1.B_TYPE= :type AND T1.B_PARTKEY=T2.P_PARTKEY";
	private static String SQL_UPDATE_BATCH_SCORE = "INSERT INTO BATCHSCORE(B_CUSTKEY,B_PARTKEY,B_SCORE,B_TYPE) "
			+ "VALUES(:custId, :prodId, :score, :type)";
	private static String SQL_DELETE_BATCH_SCORE = "DELETE FROM BATCHSCORE WHERE B_TYPE=:type";

	/**
	 * Get score result by customer id
	 * 
	 * @param customerId
	 *            the customer Id
	 * @param type
	 *            1-inDBBatch;2-ExDBBatch
	 * @return BaDemoScoreResult the score result
	 */
	public BaDemoScoreResult getScore(int customerId, int type) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("custId", customerId);
		paramMap.put("type", type);
		logger.info("CustomerId: " + customerId);
		logger.info("type: " + type);
		logger.info("Query:" + SQL_GET_SCORE);
		return getJdbcTemplate().query(SQL_GET_SCORE, paramMap,
				new BatchScoreResultSetExtractor());
	}

	/**
	 * Store batch score into BatchScore table
	 * 
	 * @param baDemoScoreResultList
	 * @param type
	 *            1-inDBBatch;2-ExDBBatch
	 */
	@SuppressWarnings("unchecked")
	public void batchUpdate(List<BaDemoScoreResult> baDemoScoreResultList,
			int type) {
		List<Map<String, Object>> paramList = new ArrayList<Map<String, Object>>();
		for (BaDemoScoreResult baDemoScoreResult : baDemoScoreResultList) {
			for (Product product : baDemoScoreResult.getProducts()) {
				HashMap<String, Object> paramMap = new HashMap<String, Object>();
				paramMap.put("custId", baDemoScoreResult.getCustomerId());
				paramMap.put("prodId", product.getProductId());
				paramMap.put("score", new BigDecimal(product.getProductScore()));
				paramMap.put("type", type);
				paramList.add(paramMap);
			}
		}
		emptyBatchScore(type);
		getJdbcTemplate().batchUpdate(SQL_UPDATE_BATCH_SCORE,
				(HashMap[]) paramList.toArray(new HashMap[paramList.size()]));
	}

	/**
	 * Empty BatchScore table
	 */
	public void emptyBatchScore(int type) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("type", type);
		getJdbcTemplate().update(SQL_DELETE_BATCH_SCORE, paramMap);
	}
}
